package com.mp.approvalreq.servlet;


import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.service.RequestServicePrx;
import com.mp.user.entity.User;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getRelatedRequestList")
public class GetRelatedRequestList extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetRelatedRequestList.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            String reqBody = getContent(req);
            logger.info("请求参数:{}", reqBody);

            Query query = JSONObject.parseObject(reqBody, Query.class);
            query.initUser(user);
            query.setVersionInfo(getVersionInfo(req));
            query.setFrom("getRelatedRequestList");

            RequestServicePrx requestServicePrx = ServantUtils.getObject(RequestServicePrx.class);
            List<Request> requests = requestServicePrx.getRelatedRequestList(query);

            returnResp(resp, requests);
        } catch (Throwable throwable) {
            onThrowable(req, resp, throwable);
        }
    }

    private void returnResp(HttpServletResponse resp, List<Request> list) throws Throwable {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "0");
        jsonObject.put("res_info", "获得成功");
        jsonObject.put("data", list);

        logger.info(jsonObject.toJSONString());

        printJson(resp, jsonObject.toJSONString());
    }
}
